﻿
    using Chain.BLL;
    using Chain.Model;
    using System;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public partial class SysParameter : PageBase
    {


        protected void BindParameter()
        {
            this.chkPwd.Checked = base.curParameter.bolPwd;
            this.ckbRegNullPwd.Checked = base.curParameter.RegNullPwd;
            this.chkMoneyAndPoint.Checked = base.curParameter.bolMoneyAndPoint;
            this.chkAutoLevel.Checked = base.curParameter.bolAutoLevel;
            this.chkDegradeLevel.Checked = base.curParameter.bolDegradeLevel;
            this.chkPastTime.Checked = base.curParameter.bolPastTime;
            this.txtRecommendPoint.Text = base.curParameter.intRecommendPoint.ToString();
            this.txtDrawMoneyPercent.Text = base.curParameter.DrawMoneyPercent.ToString();
            this.txtPointPeriod.Text = base.curParameter.intPointPeriod.ToString();
            this.txtStockCount.Text = base.curParameter.intStockCount.ToString();
            this.txtExpensePrefix.Text = base.curParameter.strExpensePrefix;
            this.txtGoodsExpensePrefix.Text = base.curParameter.strGoodsExpensePrefix;
            this.txtTimeExpensePrefix.Text = base.curParameter.strTimeExpensePrefix;
            this.txtGoodsAllotPrefix.Text = base.curParameter.strGoodsAllotPrefix;
            this.txtGoodsInPrefix.Text = base.curParameter.strGoodsInPrefix;
            this.txtMemCountPrefix.Text = base.curParameter.strMemCountPrefix;
            this.txtMemRechargePrefix.Text = base.curParameter.strMemRechargePrefix;
            this.txtMemDrawMoneyPrefix.Text = base.curParameter.strMemDrawMoneyPrefix;
            this.txtMemPointChangePrefix.Text = base.curParameter.strMemPointChangePrefix;
            this.txtGiftExchangePrefix.Text = base.curParameter.strGiftExchangePrefix;
            this.txtStorageTimingPrefix.Text = base.curParameter.StorageTimingPrefix;
            this.chkAutoPrint.Checked = base.curParameter.bolAutoPrint;
            this.chkAccordPrint.Checked = base.curParameter.bolAccordPrint;
            this.txtUnitList.Text = base.curParameter.strUnitList;
            this.chkPayCard.Checked = base.curParameter.bolIsPayCard;
            this.chkPayCash.Checked = base.curParameter.bolIsPayCash;
            this.chkPayBink.Checked = base.curParameter.bolIsPayBink;
            this.chkPayCoupon.Checked = base.curParameter.bolIsPayCoupon;
            this.txtPrintTitle.Text = base.curParameter.strPrintTitle;
            this.txtPrintFootNote.Text = base.curParameter.strPrintFootNote;
            this.chkStaff.Checked = base.curParameter.bolStaff;
            this.chkSenseiccard.Checked = base.curParameter.bolSenseiccard;
            this.chkContacticcard.Checked = base.curParameter.bolContacticcard;
            if (base.curParameter.PrintPreview == 1)
            {
                this.PrintPreview.Checked = true;
            }
            else
            {
                this.PrintPreview.Checked = false;
            }
            if (base.curParameter.PrintPaperType == 0)
            {
                this.A4ShiZhi.Checked = true;
            }
            if (base.curParameter.PrintPaperType == 1)
            {
                this.SanLianShiZhi.Checked = true;
            }
            if (base.curParameter.PrintPaperType == 2)
            {
                this.POs58ShiZhi.Checked = true;
            }
            if (base.curParameter.PrintPaperType == 3)
            {
                this.POs80ShiZhi.Checked = true;
            }
            //if (base.curParameter.AutoBackupDB)
            //{
            //    this.CKautoBackup.Checked = true;
            //}
            //else
            //{
            //    this.CKautoBackup.Checked = false;
            //}
            //this.TBautoBackupDay.Text = base.curParameter.AutoBackupDay.ToString();
            if (base.curParameter.bolStaffType)
            {
                this.rdStaff.Checked = true;
            }
            else
            {
                this.rdGoods.Checked = true;
            }
            this.chkPointLevel.Checked = base.curParameter.chkPointLevel;
            this.chkEmail.Checked = base.curParameter.bolIsEmail;
            this.chkMoneyEmail.Checked = base.curParameter.bolIsEmailNotice;
            this.txtEmailAdress.Text = base.curParameter.EmailAdress;
            this.lblEmailPwd.Text = base.curParameter.EmailPwd;
            this.txtEmailSMTP.Text = base.curParameter.EmailSMTP;
            this.txtEnterpriseEmailPort.Text = base.curParameter.EnterpriseEmailPort.ToString();
            this.txtEnterpriseEmailDisplayName.Text = base.curParameter.EnterpriseEmailDisplayName;
            this.chkEnterpriseEmailEnableSSL.Checked = base.curParameter.EnterpriseEmailEnableSSL;
            this.chkEnterpriseEmailUseDefaultCredentials.Checked = base.curParameter.EnterpriseEmailUseDefaultCredentials;
            this.txtSellerAccount.Text = base.curParameter.SellerAccount;
            this.txtPartnerID.Text = base.curParameter.PartnerID;
            this.lblPartnerKey.Text = base.curParameter.PartnerKey;
            this.chkIsNeedPwd.Checked = base.curParameter.IsEditPwdNeedOldPwd;
            this.chkMemRegisterStaff.Checked = base.curParameter.bolIsMemRegisterStaff;
            //this.chkIsMustSlotCard.Checked = base.curParameter.IsMustSlotCard;
            this.txtMemCountExpensePrefix.Text = base.curParameter.strMemCountExpensePrefix;
            this.chkIsAutoSendSMSByMemPast.Checked = base.curParameter.bolIsAutoSendSMSByMemPast;
            this.txtAutoSendSMSByMemPastForDay.Text = base.curParameter.intAutoSendSMSByMemPastForDay.ToString();
            this.chksAutoSendSMSByMemBirthday.Checked = base.curParameter.bolIsAutoSendSMSByMemBirthday;
            this.txtAutoSendSMSByMemBirthdayForDay.Text = base.curParameter.intAutoSendSMSByMemBirthdayForDay.ToString();
            if (!PubFunction.curParameter.EnableGoods)
            {
                this.lblIsStartTiming.Visible = false;
                this.lblIsStartMemCount.Visible = false;
            }
            this.chkIsStartWeiXin.Checked = base.curParameter.bolIsStartWeiXin;
            this.chkIsStartTimingProject.Checked = base.curParameter.bolIsStartTimingProject;
            this.chkIsStartMemCount.Checked = base.curParameter.bolIsStartMemCount;
            this.txtEmailUserName.Text = base.curParameter.strEmailUserName;
            this.chkIsSendCard.Checked = base.curParameter.bolIsSendCard;
            this.chkShopSmsManage.Checked = base.curParameter.bolShopSmsManage;
            this.chkShopPointManage.Checked = base.curParameter.bolShopPointManage;
            this.chkIsSettlement.Checked = base.curParameter.bolIsSettlement;
            string[] Number = PubFunction.curParameter.PointNumStr.Split(new char[] { '$' });
            this.Txthycz.Text = Number[0];
            this.Txtjfbd.Text = Number[1];
            this.Txtjfdh.Text = Number[2];
            this.Txtsprk.Text = Number[3];
            this.Txtspxf.Text = Number[4];
            this.Txtjcxf.Text = Number[5];
            this.Txtksxf.Text = Number[6];
            this.Txthycc.Text = Number[7];
            this.Txthycs.Text = Number[8];
            this.Txtxfjl.Text = Number[9];
            this.Txthyczbb.Text = Number[10];
            this.Txtjfbdbb.Text = Number[11];
            this.Txtjfdhbb.Text = Number[12];
            this.Txtckrkmx.Text = Number[13];
            this.AccountsToMoney.Text = Number[14];
        }

        protected void btnSysParameter_Click(object sender, EventArgs e)
        {
            string pointnum = "";
        Chain.BLL.SysParameter bllUpdateParameter = new Chain.BLL.SysParameter();
        Chain.Model.SysParameter modelParameter = bllUpdateParameter.GetModel(1);

        if (this.chkAutoPrint.Checked)
        {
            pointnum = ((((((((((((((pointnum + this.Txthycz.Text + "$") + this.Txtjfbd.Text + "$") + this.Txtjfdh.Text + "$") + this.Txtsprk.Text + "$") + this.Txtspxf.Text + "$") + this.Txtjcxf.Text + "$") + this.Txtksxf.Text + "$") + this.Txthycc.Text + "$") + this.Txthycs.Text + "$") + this.Txtxfjl.Text + "$") + this.Txthyczbb.Text + "$") + this.Txtjfbdbb.Text + "$") + this.Txtjfdhbb.Text + "$") + this.Txtckrkmx.Text + "$") + this.AccountsToMoney.Text + "$";

        }
        else {
            pointnum = modelParameter.PointNumStr;
        }
       
            modelParameter.ParameterID = 1;
            modelParameter.Pwd = this.chkPwd.Checked;
            modelParameter.RegNullPwd = this.ckbRegNullPwd.Checked;
            modelParameter.MoneyAndPoint = this.chkMoneyAndPoint.Checked;
            modelParameter.AutoLevel = this.chkAutoLevel.Checked;
            modelParameter.DegradeLevel = this.chkDegradeLevel.Checked;
            modelParameter.PastTime = this.chkPastTime.Checked;
            string strRecommendPoint = this.txtRecommendPoint.Text.Trim();
            decimal dclDrawMoneyPercent = 0M;
            int result = 0;
            if (!int.TryParse(this.txtStockCount.Text.Trim(), out result))
            {
                base.OutputWarn("库存报警数量只能输入正整数！");
            }
            else if (!int.TryParse(this.txtPointPeriod.Text.Trim(), out result))
            {
                base.OutputWarn("积分到期清零周期只能输入正整数！");
            }
            else if (!decimal.TryParse(this.txtDrawMoneyPercent.Text.Trim(), out dclDrawMoneyPercent))
            {
                base.OutputWarn("账户提现折损率的取值范围只能是0至1之间的数字！");
            }
            else
            {
                Regex regex = new Regex(@"^\s*([A-Za-z0-9_-]+(\.\w+)*@(\w+\.)+\w{2,5})\s*$");
                if ((this.txtEmailAdress.Text != "") && !regex.IsMatch(this.txtEmailAdress.Text))
                {
                    if (this.chkEmail.Checked)
                    {
                        base.OutputWarn("邮箱格式不正确，请重新输入！");
                        return;
                    }
                    this.txtEmailAdress.Text = "";
                }
                if (((this.txtEmailAdress.Text != "") && (((this.txtEmailPwd.Text == "") || (this.txtEmailSMTP.Text == "")) || (this.txtEnterpriseEmailPort.Text == ""))) && this.chkEmail.Checked)
                {
                    base.OutputWarn("邮箱账号已经输入，请完善邮箱设置！");
                }
                else
                {
                    if (!int.TryParse(this.txtEnterpriseEmailPort.Text.Trim(), out result))
                    {
                        if (this.chkEmail.Checked)
                        {
                            base.OutputWarn("邮件端口只能输入正整数！");
                            return;
                        }
                        this.txtEnterpriseEmailPort.Text = "25";
                    }
                    if ((dclDrawMoneyPercent <= 0M) || (dclDrawMoneyPercent > 1M))
                    {
                        base.OutputWarn("账户提现折损率的取值范围只能是0至1之间的数字！");
                    }
                    else if (!this.chkPayCard.Checked && ((!this.chkPayCash.Checked & !this.chkPayBink.Checked) & !this.chkPayCoupon.Checked))
                    {
                        base.OutputWarn("*必须选择付费方式,否则无法结算!");
                    }
                    else
                    {
                        modelParameter.DrawMoneyPercent = dclDrawMoneyPercent;
                        modelParameter.RecommendPoint = (strRecommendPoint != "") ? int.Parse(strRecommendPoint) : 0;
                        modelParameter.PointPeriod = Convert.ToInt32(this.txtPointPeriod.Text);
                        modelParameter.StockCount = Convert.ToInt32(this.txtStockCount.Text);
                        modelParameter.ExpensePrefix = this.txtExpensePrefix.Text;
                        modelParameter.GoodsExpensePrefix = this.txtGoodsExpensePrefix.Text;
                        modelParameter.TimeExpensePrefix = this.txtTimeExpensePrefix.Text;
                        modelParameter.MemCountPrefix = this.txtMemCountPrefix.Text;
                        modelParameter.MemRechargePrefix = this.txtMemRechargePrefix.Text;
                        modelParameter.GoodsInPrefix = this.txtGoodsInPrefix.Text;
                        modelParameter.GoodsAllotPrefix = this.txtGoodsAllotPrefix.Text;
                        modelParameter.MemDrawMoneyPrefix = this.txtMemDrawMoneyPrefix.Text;
                        modelParameter.MemPointChangePrefix = this.txtMemPointChangePrefix.Text;
                        modelParameter.GiftExchangePrefix = this.txtGiftExchangePrefix.Text;
                        modelParameter.Senseiccard = this.chkSenseiccard.Checked;
                        modelParameter.Contacticcard = this.chkContacticcard.Checked;
                        modelParameter.PointNumStr = pointnum;
                        if (this.PrintPreview.Checked)
                        {
                            modelParameter.PrintPreview = 1;
                        }
                        else
                        {
                            modelParameter.PrintPreview = 0;
                        }
                        if (this.A4ShiZhi.Checked)
                        {
                            modelParameter.PrintPaperType = 0;
                        }
                        if (this.SanLianShiZhi.Checked)
                        {
                            modelParameter.PrintPaperType = 1;
                        }
                        if (this.POs58ShiZhi.Checked)
                        {
                            modelParameter.PrintPaperType = 2;
                        }
                        if (this.POs80ShiZhi.Checked)
                        {
                            modelParameter.PrintPaperType = 3;
                        }
                        //if (this.CKautoBackup.Checked)
                        //{
                        //    modelParameter.AutoBackupDB = true;
                        //}
                        else
                        {
                            modelParameter.AutoBackupDB = false;
                        }
                        //modelParameter.AutoBackupDay = Convert.ToInt32(this.TBautoBackupDay.Text);
                        modelParameter.IsPayCard = this.chkPayCard.Checked;
                        modelParameter.IsPayCash = this.chkPayCash.Checked;
                        modelParameter.IsPayBink = this.chkPayBink.Checked;
                        modelParameter.IsPayCoupon = this.chkPayCoupon.Checked;
                        modelParameter.AutoPrint = this.chkAutoPrint.Checked;
                        modelParameter.AccordPrint = this.chkAccordPrint.Checked;
                        modelParameter.PrintTitle = this.txtPrintTitle.Text;
                        modelParameter.PrintFootNote = this.txtPrintFootNote.Text;
                        modelParameter.IsStaff = this.chkStaff.Checked;
                        modelParameter.IsEmail = this.chkEmail.Checked;
                        modelParameter.IsEmailNotice = this.chkMoneyEmail.Checked;
                        modelParameter.EmailAdress = this.txtEmailAdress.Text;
                        modelParameter.EmailPwd = this.txtEmailPwd.Text;
                        modelParameter.EmailSMTP = this.txtEmailSMTP.Text;
                        modelParameter.EnterpriseEmailPort = int.Parse(this.txtEnterpriseEmailPort.Text);
                        modelParameter.EnterpriseEmailEnableSSL = this.chkEnterpriseEmailEnableSSL.Checked;
                        modelParameter.EnterpriseEmailUseDefaultCredentials = this.chkEnterpriseEmailUseDefaultCredentials.Checked;
                        modelParameter.EnterpriseEmailDisplayName = this.txtEnterpriseEmailDisplayName.Text;
                        //modelParameter.IsMustSlotCard = this.chkIsMustSlotCard.Checked;
                        modelParameter.UnitList = (this.txtUnitList.Text == "") ? "个" : this.txtUnitList.Text;
                        if (this.chkStaff.Checked)
                        {
                            modelParameter.StaffType = this.rdStaff.Checked;
                        }
                        modelParameter.PointLevel = this.chkPointLevel.Checked;
                        modelParameter.SellerAccount = this.txtSellerAccount.Text;
                        modelParameter.PartnerID = this.txtPartnerID.Text;
                        modelParameter.PartnerKey = this.txtPartnerKey.Text;
                        modelParameter.IsEditPwdNeedOldPwd = this.chkIsNeedPwd.Checked;
                        modelParameter.IsMemRegisterStaff = this.chkMemRegisterStaff.Checked;
                        modelParameter.StorageTimingPrefix = this.txtStorageTimingPrefix.Text.Trim();
                        modelParameter.MemCountExpensePrefix = this.txtMemCountExpensePrefix.Text.Trim();
                        modelParameter.IsAutoSendSMSByMemPast = this.chkIsAutoSendSMSByMemPast.Checked;
                        this.txtAutoSendSMSByMemPastForDay.Text = (this.txtAutoSendSMSByMemPastForDay.Text.Trim() == "") ? "0" : this.txtAutoSendSMSByMemPastForDay.Text;
                        if (modelParameter.IsAutoSendSMSByMemPast)
                        {
                            if ((!int.TryParse(this.txtAutoSendSMSByMemPastForDay.Text.Trim(), out result) || (Convert.ToInt32(this.txtAutoSendSMSByMemPastForDay.Text.Trim()) < 0)) || (Convert.ToInt32(this.txtAutoSendSMSByMemPastForDay.Text.Trim()) > 30))
                            {
                                base.OutputWarn("会员到期提醒提前天数只能是0到30之间的正整数！");
                                return;
                            }
                            modelParameter.AutoSendSMSByMemPastForDay = Convert.ToInt32(this.txtAutoSendSMSByMemPastForDay.Text.Trim());
                        }
                        else
                        {
                            modelParameter.AutoSendSMSByMemPastForDay = 0;
                        }
                        modelParameter.IsAutoSendSMSByMemBirthday = this.chksAutoSendSMSByMemBirthday.Checked;
                        this.txtAutoSendSMSByMemBirthdayForDay.Text = (this.txtAutoSendSMSByMemBirthdayForDay.Text.Trim() == "") ? "0" : this.txtAutoSendSMSByMemBirthdayForDay.Text;
                        if (modelParameter.IsAutoSendSMSByMemBirthday)
                        {
                            if ((!int.TryParse(this.txtAutoSendSMSByMemBirthdayForDay.Text.Trim(), out result) || (Convert.ToInt32(this.txtAutoSendSMSByMemBirthdayForDay.Text.Trim()) < 0)) || (Convert.ToInt32(this.txtAutoSendSMSByMemBirthdayForDay.Text.Trim()) > 30))
                            {
                                base.OutputWarn("会员生日提醒提前天数只能是0到30之间的正整数！");
                                return;
                            }
                            modelParameter.AutoSendSMSByMemBirthdayForDay = Convert.ToInt32(this.txtAutoSendSMSByMemBirthdayForDay.Text.Trim());
                        }
                        else
                        {
                            modelParameter.AutoSendSMSByMemBirthdayForDay = 0;
                        }
                        modelParameter.IsStartWeiXin = this.chkIsStartWeiXin.Checked;
                        modelParameter.IsStartTimingProject = this.chkIsStartTimingProject.Checked;
                        modelParameter.IsStartMemCount = this.chkIsStartMemCount.Checked;
                        modelParameter.EmailUserName = this.txtEmailUserName.Text;
                        modelParameter.IsSendCard = this.chkIsSendCard.Checked;
                        modelParameter.ShopSmsManage = this.chkShopSmsManage.Checked;
                        modelParameter.ShopPointManage = this.chkShopPointManage.Checked;
                        modelParameter.ShopSettlement = this.chkIsSettlement.Checked;
                        if (!PubFunction.curParameter.EnableGoods)
                        {
                            modelParameter.IsStartTimingProject = modelParameter.IsStartMemCount = false;
                        }
                        if (!this.SwitchingMode(modelParameter))
                        {
                            base.OutputWarn("保存失败！");
                        }
                        else
                        {
                            foreach (Chain.Model.SysGroup item in new Chain.BLL.SysGroup().GetModelList(""))
                            {
                                PubFunction.UpdateGroupAuthority(item.GroupID);
                            }
                            if (bllUpdateParameter.Update(modelParameter))
                            {
                                PubFunction.curParameter = new PubFunction().LoadSysParameter();
                                base.curParameter = PubFunction.curParameter;
                                PubFunction.SaveSysLog(base._UserID, 3, "系统参数设置", "系统参数设置", base._UserShopID, DateTime.Now, PubFunction.ipAdress);
                                base.OutputWarn("保存成功！");
                            }
                            else
                            {
                                base.OutputWarn("系统异常，未保存数据，请再次点击保存！");
                            }
                        }
                    }
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                int num1 = base._UserID;
                this.BindParameter();
                if (!PubFunction.curParameter.UsingUnion)
                {
                    this.chkIsSendCard.Checked = false;
                    this.lblIsStartSendCard.Attributes.Add("style", "display:none");
                    this.chkShopSmsManage.Checked = false;
                    this.lblIsStartShopSmsMenage.Attributes.Add("style", "display:none");
                    this.chkShopPointManage.Checked = false;
                    this.lblIsStartShopPointMenage.Attributes.Add("style", "display:none");
                    this.chkIsSettlement.Checked = false;
                    this.lblIsSettlement.Attributes.Add("style", "display:none");
                }
                if (PubFunction.curParameter.istry != 1)
                {
                    //this.tab8.Style.Add("display", "none");
                }
            }
        }

        public bool SwitchingMode(Chain.Model.SysParameter modelParameter)
        {
            string strStartGood = "66,67,69,76,87,91,113,114,118";
            string strStartWeiXin = "98,120,121,122,123,124,125,126,127,128,129,130,131";
            string strStartTimingProject = "87,110,111,113,114";
            string strStartMemCount = "66,76,118";
            string strStartGoodBar = "60";
            string strStartWeiXinBar = "119";
            List<bool> name9 = new List<bool>();
            name9.Add(PubFunction.curParameter.EnableGoods);
            name9.Add(PubFunction.curParameter.EnableGoods);
            name9.Add(modelParameter.IsStartWeiXin);
            name9.Add(modelParameter.IsStartWeiXin);
            name9.Add(modelParameter.IsStartTimingProject);
            name9.Add(modelParameter.IsStartMemCount);
            List<bool> ststus = name9;
            List<string> name30 = new List<string>();
            name30.Add(strStartGoodBar);
            name30.Add(strStartGood);
            name30.Add(strStartWeiXinBar);
            name30.Add(strStartWeiXin);
            name30.Add(strStartTimingProject);
            name30.Add(strStartMemCount);
            List<string> moduleIDs = name30;
            Chain.BLL.SysParameter bllUpdateParameter = new Chain.BLL.SysParameter();
            return bllUpdateParameter.SwitchingMode(ststus, moduleIDs);
        }
    }


